package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xidian.zhiancloud.dao.OhRegisterMapper;
import com.xidian.zhiancloud.model.DeviceType;
import com.xidian.zhiancloud.model.OhRegister;
import com.xidian.zhiancloud.qo.equipFacility.OhLedgerQO;
import com.xidian.zhiancloud.vo.equipFacility.OhLedgerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备维修统计
 *
 * @Author: Summerless
 * @Date: 2021/4/2 22:45
 */
@Service
public class EquipDefendStatisticsService {

    @Autowired
    private OhRegisterMapper ohRegisterMapper;
    @Autowired
    private DeviceTypeService deviceTypeService;
    
    public Map getMaintenanceFrequency(OhLedgerQO ohLedgerQO) {
        Map map = new HashMap<>();
        Page<OhLedgerVO> page =new Page<>(0,100);
        IPage<OhLedgerVO> list = ohRegisterMapper.selectByLedgerQO(page, ohLedgerQO);
        List<OhLedgerVO> defendLedgers = list.getRecords();
        for (OhLedgerVO vo : defendLedgers) {
            Long typeId = vo.getDeviceTypeId();
            map.putIfAbsent(typeId, 0);
            map.put(typeId, (Integer)map.get(typeId) + 1);
        }
        map.forEach((key, value) -> {
            DeviceType deviceType = deviceTypeService.getById((Long) key);
            map.put(key, (deviceType == null ? "该类型已删除！" : deviceType.getName()) + "," + value.toString());
        });
        return map;
    }

    public Map getFaultTypeInfo(OhLedgerQO qo) {
        Map<String, Integer> map = new HashMap<>();
        Page<OhLedgerVO> page =new Page<>(0,100);
        IPage<OhLedgerVO> list = ohRegisterMapper.selectByLedgerQO(page, qo);
        for (OhLedgerVO record : list.getRecords()) {
            OhRegister ohRegister = ohRegisterMapper.selectById(record.getId());
            String ohResult = ohRegister.getOhResult();
            map.putIfAbsent(ohResult, 0);
            Integer quantity = map.get(ohResult);
            map.put(ohResult,  quantity + 1);
        }
        return map;
    }

}
